package com.zyf.lj.service.impl;

import com.zyf.lj.entity.News;
import com.zyf.lj.mapper.NewsMapper;
import com.zyf.lj.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NewsServiceImpl implements NewsService {

    @Autowired
    private NewsMapper newsMapper;

    @Override
    public List<News> getAllNews() {
        return newsMapper.findAllNews();
    }

    @Override
    public News createNews(News news) {
        newsMapper.insertNews(news);
        return news;
    }

    @Override
    public News getNewsById(Integer id) {
        return newsMapper.findById(id);
    }

    @Override
    public List<News> getLatestNews(int limit) {
        return newsMapper.findLatestNews(limit);
    }

    @Override
    public List<News> getNewsByUserId(Integer userId) {
        return newsMapper.findNewsByUserId(userId);
    }

    @Override
    public boolean updateNews(News news) {
        try {
            newsMapper.updateNews(news);
            return true;
        } catch (Exception e) {
            // 实际项目中应记录日志
            return false;
        }
    }

    @Override
    public boolean deleteNews(Integer id, Integer userId) {
        try {
            newsMapper.deleteNews(id, userId);
            return true;
        } catch (Exception e) {
            // 实际项目中应记录日志
            return false;
        }
    }
} 